﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace FileMerger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FileButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbs=new FolderBrowserDialog();

            if(fbs.ShowDialog(this)==DialogResult.OK)
            {
                InitFileListView(fbs.SelectedPath);
            }
        }

        private void InitFileListView(string folder)
        {
            string[] files = Directory.GetFiles(folder, "*.sql", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                FilelistView.Items.Add(file);
            }
        }

        private void ProcessButton_Click(object sender, EventArgs e)
        {
            List<string> files=new List<string>();

            foreach (ListViewItem item in FilelistView.Items)
            {
                files.Add(item.Text);
            }

            StreamWriter sw=new StreamWriter("result.sql");

            foreach (var file in files)
            {
                sw.WriteLine("--**************File: "+file+"*******************");
                sw.WriteLine(File.ReadAllText(file));
                sw.WriteLine("--**************File: " + file + " end *******************\r\n");
            }

            sw.Close();
        }
    }
}
